<?php
namespace app\api\controller;

use think\Request;
use think\App;

    /**
     * @SWG\Swagger(
     * schemes={"https","http"},
     * host="bike.wei-yong.com/api/Mingpian",
     * consumes={"multipart/form-data"},
     * produces={"application/json"},
     * @SWG\Info(
     * version="1.0",
     * title="大学城名片",
     * description="大学城名片接口文档<br> 以后就在这里愉快的对接口把!<br>"
     * ),
     *
     *@SWG\Tag(
     * name="Commonage",
     * description="公用模块",
     * ),
     * 
     * @SWG\Tag(
     * name="Image",
     * description="图片模版",
     * ),
     * @SWG\Tag(
     * name="Card",
     * description="名片模块",
     * ),
     * @SWG\Tag(
     * name="Collection",
     * description="收藏模块",
     * ),
     * @SWG\Tag(
     * name="Gift",
     * description="送礼模块",
     * ),
     * @SWG\Tag(
     * name="Exchange",
     * description="交换模块",
     * ),
     * @SWG\Tag(
     * name="Message",
     * description="消息模块",
     * ),
     * @SWG\Tag(
     * name="Top",
     * description="充值模块",
     * ),
     * @SWG\Tag(
     * name="Fabulous",
     * description="点赞模块",
     * ),
     * @SWG\Tag(
     * name="Share",
     * description="分享模块",
     * ),
     * @SWG\Tag(
     * name="Personal",
     * description="个人中心模块",
     * ),
     * @SWG\Tag(
     * name="Recharge",
     * description="充值模块",
     * ),
     * )
     */

class Mingpian extends Base
{   

    /**
      * 求两个已知经纬度之间的距离,单位为米
      * 
      * @param lng1 $ ,lng2 经度
      * @param lat1 $ ,lat2 纬度
      * @return float 距离，单位米getdistance
      * @author www.Alixixi.com 
      */
    public function getdistance($lng1,$lat1,$lng2,$lat2)
    {
        //将角度转为弧度
        $radLat1 = deg2rad($lat1);//deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
    
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
        return $s;
    }
    
    
    /**
     * @SWG\Post(
     * path="/personality",
     * tags={"Commonage"},
     * summary="个性标签/兴趣爱好/曾获荣誉",
     * description="个性标签/兴趣爱好/曾获荣誉-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="类型 1个性标签2兴趣爱好3曾获荣誉", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function personality()
    {
        $post = request()->only(['type'],'post');
        if (empty($post['type'])){
            err('类型不能为空');
        }
        
        #根据类型获取相应的数据
        $where['type'] = $post['type'];
        $personality = db('card_hobby')->field('id,name,pid')->where($where)->select();
        $personality = getCategoryList($personality,0);

        if (!$personality){
            err('empty');
        }else{
            suc('success',$personality);
        }
    }
    
    
    /**
     * @SWG\Post(
     * path="/School",
     * tags={"Commonage"},
     * summary="大学列表",
     * description="创大学列表-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="name", description="昵称", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function School()
    {
        $School = db('card_school')->field('name')->limit(100)->select();
        
        if (!$School){
            err('empty');
        }else{
            suc('success',$School);
        }
    }
    
    
    /**
     * @SWG\Post(
     * path="/recharge_list",
     * tags={"Recharge"},
     * summary="乐豆充值界面",
     * description="乐豆充值界面-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="name", type="integer", description="一级主栏目"),
     * @SWG\Header(header="children-name", type="integer", description="二级子栏目"),
     * )
     * )
     */
    public function recharge_list()
    {
        #获取乐豆余额
        $recharge = db('card_card')->field('account')->where(['user_id'=>$this->accessData['uid']])->find();
        
        if (!$recharge){
            err('empty');
        }else{
            suc('success',$recharge);
        }
    }
    
    
    /**
     * @SWG\Post(
     * path="/card_list",
     * tags={"Card"},
     * summary="名片详情",
     * description="名片详情-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="名片id", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="type", description="类型 1,只搜页面 2，名片详情页面", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="search", description="直搜条件", in="formData", required=false, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="img-image", type="integer", description="轮播图"),
     * @SWG\Header(header="card-name", type="integer", description="昵称"),
     * @SWG\Header(header="card-card", type="integer", description="名片编号"),
     * @SWG\Header(header="card-school", type="integer", description="大学名称"),
     * @SWG\Header(header="card-position", type="integer", description="专业"),
     * @SWG\Header(header="card-schooling", type="integer", description="年级"),
     * @SWG\Header(header="card-middle_school", type="integer", description="高中"),
     * @SWG\Header(header="card-class_name", type="integer", description="班级"),
     * @SWG\Header(header="card-countries", type="integer", description="国籍"),
     * @SWG\Header(header="card-ethnic", type="integer", description="民族"),
     * @SWG\Header(header="card-province", type="integer", description="家乡"),
     * @SWG\Header(header="card-faith", type="integer", description="信仰"),
     * @SWG\Header(header="card-animal", type="integer", description="属相"),
     * @SWG\Header(header="card-height", type="integer", description="身高"),
     * @SWG\Header(header="card-constellation", type="integer", description="星座"),
     * @SWG\Header(header="card-weight", type="integer", description="体重"),
     * @SWG\Header(header="card-birthday", type="integer", description="生日"),
     * @SWG\Header(header="card-blood_type", type="integer", description="血型"),
     * @SWG\Header(header="card-personality", type="integer", description="消息标题"),
     * @SWG\Header(header="card-hobbies", type="integer", description="兴趣爱好"),
     * @SWG\Header(header="card-idol", type="integer", description="崇拜偶像"),
     * @SWG\Header(header="card-title", type="integer", description="职务头衔"),
     * @SWG\Header(header="card-group", type="integer", description="加入团体"),
     * @SWG\Header(header="card-honor", type="integer", description="曾获荣誉"),
     * @SWG\Header(header="card-username", type="integer", description="真实姓名"),
     * @SWG\Header(header="card-number", type="integer", description="个人学号"),
     * @SWG\Header(header="card-weixin", type="integer", description="联系方式"),
     * @SWG\Header(header="card-content", type="integer", description="个性签名"),
     * @SWG\Header(header="card-theme", type="integer", description="名片主题1-2-3-4"),
     * @SWG\Header(header="card-charm", type="integer", description="魅力值"),
     * @SWG\Header(header="card-collection_number", type="integer", description="收藏数量"),
     * @SWG\Header(header="card-share_number", type="integer", description="分享数量"),
     * @SWG\Header(header="card-like_number", type="integer", description="点赞数量"),
     * @SWG\Header(header="card-ritual_number", type="integer", description="送礼数量"),
     * @SWG\Header(header="card-is", type="integer", description="是否交换名片0未交换1已交换"),
     * @SWG\Header(header="card-collection", type="integer", description="是否收藏过0未收藏1已收藏"),
     * @SWG\Header(header="card-fabulous", type="integer", description="是否点赞0未点赞1已点赞"),
     * @SWG\Header(header="gift-img", type="integer", description="礼物图片"),
     * @SWG\Header(header="gift-number", type="integer", description="礼物数量"),
     * @SWG\Header(header="card-totalGift", type="integer", description="收礼物总数量"),
     * )
     * )
     */
    public function card_list()
    {
        $post = request()->only(['id','search','type'],'post');
        
        $where = [];
        if (empty($post['type'])){
            err('入口类型不能为空');
        }elseif($post['type'] == 2){
            #名片详情
            if (empty($post['id'])){
                err('名片id不能为空');
            }
            $where['id'] = $post['id'];
        }elseif($post['type'] == 1){
            #直搜
            if (empty($post['search'])){
                err('直搜条件不能为空');
            }
            $where['card|name'] = $post['search'];
        }
         
        #轮播图
        $card_list['img'] = db('card_img')->field('image')->where(['type'=>1])->order("sorts desc")->select();
        
        #名片
        $where['state'] = 2;//必须审核通过
        $field = "id,user_id,name,card,school,position,schooling,middle_school,class_name,countries,ethnic,province,faith,
                 animal,height,constellation,weight,birthday,blood_type,personality,hobbies,idol,title,group,
                 honor,username,number,weixin,content,content,theme,charm,collection_number,share_number,like_number,
                 ritual_number";
        $card_list['card'] = db('card_card')->field($field)->where($where)->find();
        
        if (!$card_list['card']){
            err('请从新搜索');
        }
        
        $card_list['card']['is'] = 1;//0代表未交换名片 1代表已交换

        #判断是否和他交换过名片，显示联系方式
        $exchange = db('card_exchange')
            ->where(['user_id'=>$this->accessData['uid'],'type_user_id'=>$card_list['card']['user_id']])->find();
        
        if (!$exchange){
            unset($card_list['card']['username']);
            unset($card_list['card']['number']);
            unset($card_list['card']['weixin']);
            $card_list['card']['is'] = 0;//0代表未交换名片 1代表已交换
        }
        
        #查询自己的本片
        $my_card = db('card_card')->where(['user_id'=>$this->accessData['uid']])->find();

        #计算同频值
        $card_list['card']['frequency'] = 0;
        #体重
        if ($my_card['weight'] == $card_list['card']['weight']){
            $card_list['card']['frequency'] += 1;
        }
        #学校
        if ($my_card['school'] == $card_list['card']['school']){
            $card_list['card']['frequency'] += 1;
        }
        #家乡
        if ($my_card['province'] == $card_list['card']['province']){
            $card_list['card']['frequency'] += 1;
        }
        #专业
        if ($my_card['position'] == $card_list['card']['position']){
            $card_list['card']['frequency'] += 1;
        }
        #年级
        if ($my_card['schooling'] == $card_list['card']['schooling']){
            $card_list['card']['frequency'] += 1;
        }
        #高中
        if ($my_card['middle_school'] == $card_list['card']['middle_school']){
            $card_list['card']['frequency'] += 1;
        }
        #班级
        if ($my_card['class_name'] == $card_list['card']['class_name']){
            $card_list['card']['frequency'] += 1;
        }
        #国籍
        if ($my_card['countries'] == $card_list['card']['countries']){
            $card_list['card']['frequency'] += 1;
        }
        #民族
        if ($my_card['ethnic'] == $card_list['card']['ethnic']){
            $card_list['card']['frequency'] += 1;
        }
        #信仰
        if ($my_card['faith'] == $card_list['card']['faith']){
            $card_list['card']['frequency'] += 1;
        }
        #生日
        if ($my_card['birthday'] == $card_list['card']['birthday']){
            $card_list['card']['frequency'] += 1;
        }
        #属相
        if ($my_card['animal'] == $card_list['card']['animal']){
            $card_list['card']['frequency'] += 1;
        }
        #星座
        if ($my_card['constellation'] == $card_list['card']['constellation']){
            $card_list['card']['frequency'] += 1;
        }
        #身高
        if ($my_card['height'] == $card_list['card']['height']){
            $card_list['card']['frequency'] += 1;
        }
        #血型
        if ($my_card['blood_type'] == $card_list['card']['blood_type']){
            $card_list['card']['frequency'] += 1;
        }
        #个性标签
        if ($my_card['personality'] == $card_list['card']['personality']){
            $card_list['card']['frequency'] += 1;
        }
        #兴趣爱好
        if ($my_card['hobbies'] == $card_list['card']['hobbies']){
            $card_list['card']['frequency'] += 1;
        }
        #崇拜偶像
        if ($my_card['idol'] == $card_list['card']['idol']){
            $card_list['card']['frequency'] += 1;
        }
        #职务头衔
        if ($my_card['title'] == $card_list['card']['title']){
            $card_list['card']['frequency'] += 1;
        }
        #加入团队
        if ($my_card['group'] == $card_list['card']['group']){
            $card_list['card']['frequency'] += 1;
        }
        #曾获荣誉
        if ($my_card['honor'] == $card_list['card']['honor']){
            $card_list['card']['frequency'] += 1;
        }
        #家乡
        if ($my_card['province'] == $card_list['card']['province']){
            $card_list['card']['frequency'] += 1;
        }
        
        $where = [];
        $where['user_id'] = $this->accessData['uid'];
        $where['type_user_id'] = $card_list['card']['id'];
        #判断是否收藏过
        $card_list['card']['collection'] = 0;//未收藏
        $collection = db('card_collection')->where($where)->find();
        if ($collection){
            $card_list['card']['collection'] = 1;//已收藏
        }
        
        #判断是否点赞过
        $card_list['card']['fabulous'] = 0;//未点赞
        $collection = db('card_fabulous')->where($where)->find();
        if ($collection){
            $card_list['card']['fabulous'] = 1;//已点赞
        }
        
        #他收到的礼物
        $card_list['gift'] = db('card_gift')
		->alias("a")
        ->field('a.id,a.name,a.image,(select count(*) from applet_card_gift_giving as b where a.id = b.gift_id and b.type_user_id = "'.$card_list['card']['user_id'].'") as number')
		->order("a.sorts desc")
        ->select();
		$card_list['card']['totalGift'] = 0;
		foreach($card_list['gift'] as $key => $val){
			$card_list['card']['totalGift'] += $val["number"];
		}
        if (!$card_list['gift']){
            $card_list['gift'] = [];
        }
        
        if (!$card_list){
            err('empty');
        }else{
            suc('success',$card_list);
        }
    }
    
    
    /**
     * @SWG\Post(
     * path="/gift_list",
     * tags={"Gift"},
     * summary="礼物列表",
     * description="礼物列表-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="pagesize", description="页数", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="name", type="id", description="礼物id"),
     * @SWG\Header(header="name", type="name", description="礼物名称"),
     * @SWG\Header(header="name", type="image", description="礼物图标"),
     * @SWG\Header(header="name", type="money", description="礼物价格"),
     * )
     * )
     */
    public function gift_list()
    {
        $post = request()->only(['page','pagesize'],'post');
        
        //表单验证
        $result = $this->validate($post, 'Mingpian.page');
        
        if (true !== $result) {
            err($result);
        }
        
        #查询语句-礼物信息
        $gift = db('card_gift')->order('sorts asc')->page($post['page'],$post['pagesize'])->select();
        
        if (!$gift){
            err('empty');
        }else{
            suc('success',$gift);
        }
    }
    
    
    /**
     * @SWG\Post(
     * path="/share_list",
     * tags={"Share"},
     * summary="分享",
     * description="分享-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="名片ID", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function share_list()
    {
        $post = request()->only(['id'],'post');
        
        if (empty($post['id'])){
            err('名片id不能为空');
        }
        
        $data['user_id'] = $this->accessData['uid'];
        $data['type_user_id'] = $post['id'];
        $data['create_time'] = date("Y-m-d H:i:s");
        #分享记录增加语句
        $share = db('card_share')->insert($data);
        
        if ($share < 1){
            err('失败');
        }else{
            #分享成功，名片分享数+1
            db('card_card')->where(['id'=>$post['id']])->setInc('share_number');
            suc('success');
        }
    }
    
    /**
     * @SWG\Post(
     * path="/card_search",
     * tags={"Card"},
     * summary="名片搜索",
     * description="名片搜索-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="log", description="经度", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="lat", description="纬度", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="sex", description="性别1男2女", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="theme", description="主题1-2-3-4", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="hobbies", description="曾获荣誉", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="school", description="学校", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="schooling", description="年级", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="animal", description="属相", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="constellation", description="星座", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="blood_type", description="血型", in="formData", required=false, type="string"),
     * @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="pagesize", description="页数", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="类型 1,按同频2,按魅力3,按距离", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="name", type="id", description="名片id"),
     * @SWG\Header(header="name", type="image", description="头像"),
     * @SWG\Header(header="name", type="name", description="昵称"),
     * @SWG\Header(header="name", type="sex", description="性别"),
     * @SWG\Header(header="name", type="position", description="专业"),
     * @SWG\Header(header="name", type="birthday", description="生日"),
     * @SWG\Header(header="name", type="height", description="身高"),
     * @SWG\Header(header="name", type="countries", description="国籍"),
     * @SWG\Header(header="name", type="ethnic", description="民族"),
     * @SWG\Header(header="name", type="faith", description="信仰"),
     * @SWG\Header(header="name", type="charm", description="魅力值"),
     * @SWG\Header(header="name", type="school", description="学校"),
     * )
     * )
     */
    public function card_search()
    {
        $post = request()->only(['sex','theme','hobbies','school','schooling','animal','constellation',
        'blood_type','page','pagesize','log','lat','type'],'post');
        
        if (empty($post['type'])){
            $post['type'] = 1;
        }
        //表单验证
        $result = $this->validate($post, 'Mingpian.card_search');
        
        if (true !== $result) {
            err($result);
        }
        
        if (empty($post['type'])){
            err('类型不能为空');
        }
        
        //搜索符合条件的名片(必须满足认证)
        $where = [];
        if (!empty($post['sex'])){
            $where['a.sex'] = $post['sex'];
        }
        if (!empty($post['theme'])){
            $where['a.theme'] = $post['theme'];
        }
        if (!empty($post['hobbies'])){
            $where['a.hobbies'] = $post['hobbies'];
        }
        if (!empty($post['school'])){
            $where['a.school'] = $post['school'];
        }
        if (!empty($post['schooling'])){
            $where['a.schooling'] = $post['schooling'];
        }
        if (!empty($post['animal'])){
            $where['a.animal'] = $post['animal'];
        }
        if (!empty($post['constellation'])){
            $where['a.constellation'] = $post['constellation'];
        }
        if (!empty($post['blood_type'])){
            $where['a.blood_type'] = $post['blood_type'];
        }
        
        $where['a.state'] = 2;//认证通过
        $where['a.user_id'] = ['neq',$this->accessData['uid']];//排除自己的名片
        $search = db('card_card')->alias('a')->join('card_school b','a.school = b.name','left')
                    ->where($where)
                    ->field('a.*,b.log,b.lat')
                    ->page($post['page'],$post['pagesize'])
                    ->select();

        if (!$search){
            err('暂无数据');
        }
        
        //查询自己的本片
        $my_card = db('card_card')->where(['user_id'=>$this->accessData['uid']])->find();
 
        //计算同频值
        if (!$my_card){
            foreach ($search as $k => $v){
                    $search[$k]['frequency'] = 0;
            } 
        }else{
            foreach ($search as $k => $v){
                $search[$k]['frequency'] = 0;
                    #体重
                    if ($my_card['weight'] == $v['weight']){
                        $search[$k]['frequency'] += 1;
                    }
                    #学校
                    if ($my_card['school'] == $v['school']){
                        $search[$k]['frequency'] += 1;
                    }
                    #家乡
                    if ($my_card['province'] == $v['province']){
                        $search[$k]['frequency'] += 1;
                    }
                    #专业
                    if ($my_card['position'] == $v['position']){
                        $search[$k]['frequency'] += 1;
                    }
                    #年级
                    if ($my_card['schooling'] == $v['schooling']){
                        $search[$k]['frequency'] += 1;
                    }
                    #高中
                    if ($my_card['middle_school'] == $v['middle_school']){
                        $search[$k]['frequency'] += 1;
                    }
                    #班级
                    if ($my_card['class_name'] == $v['class_name']){
                        $search[$k]['frequency'] += 1;
                    }
                    #国籍
                    if ($my_card['countries'] == $v['countries']){
                        $search[$k]['frequency'] += 1;
                    }
                    #民族
                    if ($my_card['ethnic'] == $v['ethnic']){
                        $search[$k]['frequency'] += 1;
                    }
                    #信仰
                    if ($my_card['faith'] == $v['faith']){
                        $search[$k]['frequency'] += 1;
                    }
                    #生日
                    if ($my_card['birthday'] == $v['birthday']){
                        $search[$k]['frequency'] += 1;
                    }
                    #属相
                    if ($my_card['animal'] == $v['animal']){
                        $search[$k]['frequency'] += 1;
                    }
                    #星座
                    if ($my_card['constellation'] == $v['constellation']){
                        $search[$k]['frequency'] += 1;
                    }
                    #身高
                    if ($my_card['height'] == $v['height']){
                        $search[$k]['frequency'] += 1;
                    }
                    #血型
                    if ($my_card['blood_type'] == $v['blood_type']){
                        $search[$k]['frequency'] += 1;
                    }
                    #个性标签
                    if ($my_card['personality'] == $v['personality']){
                        $search[$k]['frequency'] += 1;
                    }
                    #兴趣爱好
                    if ($my_card['hobbies'] == $v['hobbies']){
                        $search[$k]['frequency'] += 1;
                    }
                    #崇拜偶像
                    if ($my_card['idol'] == $v['idol']){
                        $search[$k]['frequency'] += 1;
                    }
                    #职务头衔
                    if ($my_card['title'] == $v['title']){
                        $search[$k]['frequency'] += 1;
                    }
                    #加入团队
                    if ($my_card['group'] == $v['group']){
                        $search[$k]['frequency'] += 1;
                    }
                    #曾获荣誉
                    if ($my_card['honor'] == $v['honor']){
                        $search[$k]['frequency'] += 1;
                    }
                    #家乡
                    if ($my_card['province'] == $v['province']){
                        $search[$k]['frequency'] += 1;
                    }
            }
        }
        
        #计算经纬度距离并且排序
        foreach ($search as $k => $v){
            $search[$k]['km'] = $this->getdistance($v['log'],$v['lat'],$post['log'],$post['lat']);
            $search[$k]['km'] = intval($search[$k]['km']/1000);//米转换千米
        }

        #距离排序/分类排序
        if ($post['type'] == 1){
            $sort = array('direction'=>'SORT_DESC','field'=>'frequency');//按同频降序
        }elseif($post['type'] == 2){
            $sort = array('direction'=>'SORT_DESC','field'=>'charm');//按魅力降序
        }else{
            $sort = array('direction'=>'SORT_ASC','field'=>'km');//按 距离升序
        }
        
        $arrSort = array();
        foreach($search as $uniqid => $row){
            foreach($row AS $key => $value){
                $arrSort[$key][$uniqid] = $value;
            }
        }
        if($sort['direction']){
            array_multisort($arrSort[$sort['field']], constant($sort['direction']), $search);
        }
        
        
        if (!$search){
            err('empty');
        }else{
            suc('success',$search);
        }
        
    }
    
    /**
     * @SWG\Post(
     * path="/My_hidden",
     * tags={"Card"},
     * summary="名片隐藏设置",
     * description="名片隐藏设置-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="search_status", description="1名片搜索 2名片直搜3全部显示0全部隐藏", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function My_hidden()
    {
        $post = request()->only(['search_status'],'post');
        if (empty($post['search_status'])){
            $post['search_status'] = 3;
        }
        
        $hidden = db('card_card')->where(['user_id'=>$this->accessData['uid']])->update(['search_status'=>$post['search_status']]);
        
        if (!$hidden){
            err('error');
        }else{
            suc('success');
        }
    }
    
    /**
     * @SWG\Post(
     * path="/giving",
     * tags={"Gift"},
     * summary="送礼",
     * description="送礼-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="名片ID", in="formData", required=true, type="string"),
     * SWG\Parameter(name="giving_id", description="礼物ID", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function giving()
    {
        $post = request()->only(['id','giving_id'],'post');
        if (empty($post['id'])){
            err('名片ID不能为空');
        }
        
        if (empty($post['giving_id'])){
            err('礼物ID不能为空');
        }
        
        #获得礼物信息
        $giving = db('card_gift')->field('name,image,money')->where(['id'=>$post['giving_id']])->find();
        $data['gift_id'] = $post['giving_id'];//礼物id
        $data['title'] = $giving['name'];//礼物名称
        $data['img'] = $giving['image'];//礼物图标
        $data['money'] = $giving['money'];//礼物价格
        $data['user_id'] = $this->accessData['uid'];//用户id
        
        #获取名片用户
        $type_user_id = db('card_card')->field('user_id')->where(['id'=>$post['id']])->find();
        $data['type_user_id'] = $type_user_id['user_id'];//收礼人id
        $data['create_time'] = date("Y-m-d H:i:s");//送礼时间
        
        #增加语句
        $collection = db('card_gift_giving')->insert($data);
        
        if ($collection < 1){
            err('失败');
        }else{
            #送礼成功，名片送礼数+1/魅力值+1
            db('card_card')->where(['id'=>$post['id']])->setInc('ritual_number');
            db('card_card')->where(['id'=>$post['id']])->setInc('charm');
            suc('success');
        }
    }
    
    /**
     * @SWG\Post(
     * path="/My_giving",
     * tags={"Gift"},
     * summary="送礼记录",
     * description="送礼记录-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="1 我送出的礼物 2 我收到的礼物", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="pagesize", description="页数", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="列表ID"),
     * @SWG\Header(header="name", type="integer", description="送礼人昵称/收礼人昵称"),
     * @SWG\Header(header="money", type="integer", description="礼物价格"),
     * @SWG\Header(header="create_time", type="integer", description="送礼时间"),
     * @SWG\Header(header="img", type="integer", description="礼物图片"),
     * @SWG\Header(header="title", type="integer", description="礼物名称"),
     * )
     * )
     */
    public function My_giving()
    {
        $post = request()->only(['type','page','pagesize'],'post');
        
        if (empty($post['type'])){
            $post['type'] = 1;
        }
        
        //表单验证
        $result = $this->validate($post, 'Mingpian.page');
        
        if (true !== $result) {
            err($result);
        }
        
        if ($post['type'] == 1){
            $giving = db('card_gift_giving')->alias('a')->join('card_card b','a.type_user_id = b.user_id','left')
            ->field('a.id,a.create_time,a.title,a.img,a.money,b.name')
            ->where(['a.user_id'=>$this->accessData['uid']])
            ->page($post['page'],$post['pagesize'])
            ->select();
        
        }else {
            $giving = db('card_gift_giving')->alias('a')->join('card_card b','a.user_id = b.user_id','left')
            ->field('a.id,a.create_time,a.title,a.img,a.money,b.name')
            ->where(['a.type_user_id'=>$this->accessData['uid']])
            ->page($post['page'],$post['pagesize'])
            ->select();
        }
        if (!$giving){
            err('empty');
        }else{
            suc('success',$giving);
        }
    }
    
    /**
     * @SWG\Post(
     * path="/fabulous",
     * tags={"Fabulous"},
     * summary="点赞",
     * description="点赞-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="名片ID", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function fabulous()
    {
        $post = request()->only(['id'],'post');
        if (empty($post['id'])){
            err('名片ID不能为空');
        }
        
        $type_user_id = db('card_card')->field('user_id')->where(['id'=>$post['id']])->find();
        
        #判断是否重复点赞
        $fabulous = db('card_fabulous')->where(['user_id'=>$this->accessData['uid'],'type_user_id'=>$type_user_id['user_id']])->find();
        if ($fabulous){
            err('请勿重复点赞');
        }
        
        $data['user_id'] = $this->accessData['uid'];
        $data['type_user_id'] = $type_user_id['user_id'];
        $data['create_time'] = date("Y-m-d H:i:s");
        
        #点赞增加语句
        $collection = db('card_fabulous')->insert($data);
        
        if ($collection < 1){
            err('失败');
        }else{
            #点赞成功，名片点赞数+1
            db('card_card')->where(['id'=>$post['id']])->setInc('like_number');
            suc('success');
        }
    }
    
    /**
     * @SWG\Post(
     * path="/collection",
     * tags={"Collection"},
     * summary="收藏",
     * description="收藏-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="名片ID", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function collection()
    {
        $post = request()->only(['id'],'post');
        if (empty($post['id'])){
            err('名片ID不能为空');
        }
        
        $type_user_id = db('card_card')->field('user_id')->where(['id'=>$post['id']])->find();
        
        #判断是否重复收藏
        $res = db('card_collection')->where(['user_id'=>$this->accessData['uid'],'type_user_id'=>$type_user_id['user_id']])->find();
        
        if ($res){
            err('请勿重复收藏');
        }
        
        $data['user_id'] = $this->accessData['uid'];
        $data['type_user_id'] = $type_user_id['user_id'];
        $data['create_time'] = date("Y-m-d H:i:s");
        
        #收藏增加语句
        $collection = db('card_collection')->insert($data);
        
        if ($collection < 1){
            err('失败');
        }else{
            #收藏成功，名片收藏数+1
            db('card_card')->where(['id'=>$post['id']])->setInc('collection_number');
            suc('success');
        }
        
    }
   
    /**
     * @SWG\Post(
     * path="/My_collection",
     * tags={"Collection"},
     * summary="收藏记录",
     * description="收藏记录-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="1 我收藏了谁 2 谁收藏了我", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="pagesize", description="页数", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="列表ID"),
     * @SWG\Header(header="name", type="integer", description="昵称"),
     * @SWG\Header(header="image", type="integer", description="头像"),
     * @SWG\Header(header="create_time", type="integer", description="收藏时间"),
     * @SWG\Header(header="sex", type="integer", description="性别1男2女"),
     * @SWG\Header(header="school", type="integer", description="大学"),
     * @SWG\Header(header="schooling", type="integer", description="年级(学历)"),
     * )
     * )
     */
    public function My_collection()
    {
        $post = request()->only(['type','page','pagesize'],'post');
        
        if (empty($post['type'])){
            $post['type'] = 1;
        }
        
        //表单验证
        $result = $this->validate($post, 'Mingpian.page');
        
        if (true !== $result) {
            err($result);
        }
        
        if ($post['type'] == 1){
            $collection = db('card_collection')->alias('a')->join('card_card b','a.type_user_id = b.user_id','left')
                        ->field('a.id,a.create_time,b.image,b.name,b.sex,b.school,b.schooling')
                        ->where(['a.user_id'=>$this->accessData['uid']])
                        ->page($post['page'],$post['pagesize'])
                        ->select();
                          
        }else {
            $collection = db('card_collection')->alias('a')->join('card_card b','a.user_id = b.user_id','left')
            ->field('a.id,a.create_time,b.image,b.name,b.sex,b.school,b.schooling')
            ->where(['a.type_user_id'=>$this->accessData['uid']])
            ->page($post['page'],$post['pagesize'])
            ->select();
        }
        if (!$collection){
            err('empty');
        }else{
            suc('success',$collection);
        }
    }
    
  
    /**
     * @SWG\Post(
     * path="/exchange",
     * tags={"Exchange"},
     * summary="交换",
     * description="交换-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="名片id", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="theme", description="主题1大学好友 2创业伙伴 3校园情侣 4终身伴侣", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function exchange()
    {
        $post = request()->only(['id','theme'],'post');
        
        if (empty($post['id'])){
            err('名片id不能为空');
        }
        if (empty($post['theme'])){
            err('名片主题不能为空');
        }
        if ($post['id'] == $this->accessData['uid']){
            err('不能跟自己互换');
        }
		
        $res = db('card_card')->field('user_id,theme')->where(['id'=>$post['id']])->find();
        $theme = $res['theme'];
        #判断是否重复交换
        $where['user_id'] = $this->accessData['uid'];
        $where['type_user_id'] = $res['user_id'];
        $where['theme'] = $theme;
        $my_exchange = db('card_exchange')->where($where)->find();
        
        if ($my_exchange){
            err('已经交换，请勿重复交换');
        }
        
        #交换主题
        $data['user_id'] = $this->accessData['uid'];
        $data['type_user_id'] = $res['user_id'];
        $data['theme'] = $post['theme'];
        $data['create_time'] = date("Y-m-d H::s");
        $data['state'] = 0;
        $exchange = db('card_exchange')->insert($data);
        
        if ($exchange < 1){
            err('失败');
        }else{
            suc('success');
        }
        
    }
    
    /**
     * @SWG\Post(
     * path="/My_exchange",
     * tags={"Exchange"},
     * summary="交换记录",
     * description="交换记录-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="1 我与谁交换 2 谁与我交换 ", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="pagesize", description="页数", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="id", type="integer", description="列表ID"),
     * @SWG\Header(header="name", type="integer", description="昵称"),
     * @SWG\Header(header="image", type="integer", description="头像"),
     * @SWG\Header(header="create_time", type="integer", description="交换时间"),
     * @SWG\Header(header="sex", type="integer", description="性别1男2女"),
     * @SWG\Header(header="state", type="integer", description="0等待1成功2失败"),
     * @SWG\Header(header="theme", type="integer", description="主题1大学好友2创业伙伴3校园情侣4终生伴侣"),
     * )
     * )
     */
    public function My_exchange()
    {
        $post = request()->only(['type','page','pagesize'],'post');
        
        if (empty($post['type'])){
            $post['type'] = 1;
        }
        
        //表单验证
        $result = $this->validate($post, 'Mingpian.page');
        
        if (true !== $result) {
            err($result);
        }
        
        if ($post['type'] == 1){
            $exchange = db('card_exchange')->alias('a')->join('card_card b','a.type_user_id = b.user_id','left')
            ->field('a.id,b.name,b.image,a.create_time,b.sex,a.state,a.theme')
            ->where(['a.user_id'=>$this->accessData['uid']])
            ->page($post['page'],$post['pagesize'])->select();
        }else{
            $exchange = db('card_exchange')->alias('a')->join('card_card b','a.type_user_id = b.user_id','left')
            ->field('a.id,b.name,b.image,a.create_time,b.sex,a.state,a.theme')
            ->where(['a.type_user_id'=>$this->accessData['uid']])
            ->page($post['page'],$post['pagesize'])->select();
        }
        
        if (!$exchange){
            err('empty');
        }else{
            suc('success',$exchange);
        }
        
    }
    
    /**
     * @SWG\Post(
     * path="/accept_exchange",
     * tags={"Exchange"},
     * summary="接受交换邀请",
     * description="接受交换邀请-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="id", description="列表id", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="state", description="1接受 2拒绝", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function accept_exchange()
    {
        $post = request()->only(['id','state'],'post');
        
        if (empty($post['id'])){
            err('列表id不能为空');
        }
        if (empty($post['state'])){
            err('状态不能为空');
        }
        
        $accept = db('card_exchange')->where(['id'=>$post['id']])->update(['state'=>$post['state']]);
        
        if (!$accept){
            err('error');
        }else{
            suc('success');
        }
    }
    
    /**
     * @SWG\Post(
     * path="/my_card",
     * tags={"Card"},
     * summary="我的名片",
     * description="我的名片-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="img-image", type="integer", description="轮播图"),
     * @SWG\Header(header="card-name", type="integer", description="昵称"),
     * @SWG\Header(header="card-card", type="integer", description="名片编号"),
     * @SWG\Header(header="card-school", type="integer", description="大学名称"),
     * @SWG\Header(header="card-position", type="integer", description="专业"),
     * @SWG\Header(header="card-schooling", type="integer", description="年级"),
     * @SWG\Header(header="card-middle_school", type="integer", description="高中"),
     * @SWG\Header(header="card-class_name", type="integer", description="班级"),
     * @SWG\Header(header="card-countries", type="integer", description="国籍"),
     * @SWG\Header(header="card-ethnic", type="integer", description="民族"),
     * @SWG\Header(header="card-province", type="integer", description="家乡"),
     * @SWG\Header(header="card-faith", type="integer", description="信仰"),
     * @SWG\Header(header="card-animal", type="integer", description="属相"),
     * @SWG\Header(header="card-height", type="integer", description="身高"),
     * @SWG\Header(header="card-constellation", type="integer", description="星座"),
     * @SWG\Header(header="card-weight", type="integer", description="体重"),
     * @SWG\Header(header="card-birthday", type="integer", description="生日"),
     * @SWG\Header(header="card-blood_type", type="integer", description="血型"),
     * @SWG\Header(header="card-personality", type="integer", description="消息标题"),
     * @SWG\Header(header="card-hobbies", type="integer", description="兴趣爱好"),
     * @SWG\Header(header="card-idol", type="integer", description="崇拜偶像"),
     * @SWG\Header(header="card-title", type="integer", description="职务头衔"),
     * @SWG\Header(header="card-group", type="integer", description="加入团体"),
     * @SWG\Header(header="card-honor", type="integer", description="曾获荣誉"),
     * @SWG\Header(header="card-username", type="integer", description="真实姓名"),
     * @SWG\Header(header="card-number", type="integer", description="个人学号"),
     * @SWG\Header(header="card-weixin", type="integer", description="联系方式"),
     * @SWG\Header(header="card-content", type="integer", description="个性签名"),
     * @SWG\Header(header="card-theme", type="integer", description="名片主题1-2-3-4"),
     * @SWG\Header(header="card-charm", type="integer", description="魅力值"),
     * @SWG\Header(header="card-collection_number", type="integer", description="收藏数量"),
     * @SWG\Header(header="card-share_number", type="integer", description="分享数量"),
     * @SWG\Header(header="card-like_number", type="integer", description="点赞数量"),
     * @SWG\Header(header="card-ritual_number", type="integer", description="送礼数量"),
     * @SWG\Header(header="gift-img", type="integer", description="礼物图片"),
     * @SWG\Header(header="gift-number", type="integer", description="礼物数量"),
     * )
     * )
     */
    public function my_card()
    {
        #wo的名片轮播图
        $my_card['img'] = db('card_img')->field('image')->where(['type'=>1])->order("sorts desc")->select();
        
        #我的名片
        $field = 'name,card,school,position,schooling,middle_school,class_name,countries,ethnic,province,faith,
                  animal,height,constellation,weight,birthday,blood_type,personality,hobbies,idol,title,group,
                  honor,username,number,weixin,content,theme,charm,collection_number,share_number,like_number,
                  ritual_number';
        $my_card['card'] = db('card_card')->field($field)->where(['user_id'=>$this->accessData['uid']])->find();
        $my_card['card']['personality'] = explode(",",$my_card['card']['personality']);
        $my_card['card']['hobbies'] = explode(",",$my_card['card']['hobbies']);
        $my_card['card']['idol'] = explode(",",$my_card['card']['idol']);
        $my_card['card']['title'] = explode(",",$my_card['card']['title']);
        $my_card['card']['group'] = explode(",",$my_card['card']['group']);
        $my_card['card']['honor'] = explode(",",$my_card['card']['honor']);

        if (!$my_card['card']){
            $my_card['card'] = [];
        }
        
        #收到的礼物
        $my_card['gift'] = db('card_gift_giving')
                           ->field('img,count(gift_id) as number')
                           ->where(['type_user_id'=>$this->accessData['uid']])
                           ->group('gift_id')
                           ->select();
                           
        if (!$my_card['gift']){
            $my_card['gift'] = [];
        }
        
        suc('',$my_card);
    }
    
    /**
     * @SWG\Post(
     * path="/card_record",
     * tags={"Message"},
     * summary="我的消息",
     * description="我的消息-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="type", description="类型0系统消息 1后台消息", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="page", description="页码", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="pagesize", description="页数", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="title", type="integer", description="消息标题"),
     * @SWG\Header(header="content", type="integer", description="消息内容"),
     * @SWG\Header(header="create_time", type="integer", description="消息时间"),
     * )
     * )
     */
    public function card_record()
    {
        $post = request()->only(['type','page','pagesize'],'post');
        
        if (empty($post['type'])){
            $post['type'] = 0;
        }
        
        //表单验证
        $result = $this->validate($post, 'Mingpian.page');
        
        if (true !== $result) {
            err($result);
        }
        
        $record = db('card_message_record')->field('title,content,create_time')
                                      ->where(['user_id'=>$this->accessData['uid'],'type'=>$post['type']])
                                      ->page($post['page'],$post['pagesize'])
                                      ->select();
        
        if (!$record){
            err('empty');
        }else{
            suc('success',$record);
        }
    }
    
    
    /**
     * @SWG\Post(
     * path="/Personal",
     * tags={"Personal"},
     * summary="个人中心",
     * description="个人中心-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * @SWG\Header(header="image", type="integer", description="头像"),
     * @SWG\Header(header="username", type="integer", description="昵称"),
     * @SWG\Header(header="card", type="integer", description="名片编号"),
     * @SWG\Header(header="state", type="integer", description="身份认证状态0未认证 1待认证 2认证通过 3认证失败"),
     * @SWG\Header(header="account", type="integer", description="乐豆余额"),
     * )
     * )
     */
    public function Personal()
    {
        //查询名片表
        $Personal = db('card_card')->field('card,username,image,state,account')->where(['user_id'=>$this->accessData['uid']])->find();
        
        //存在返回数据，不存在查询user表
        if ($Personal){
            suc('success',$Personal);
        }else{
            $Personal = db('user')->field('avatarurl as image,nickname as username')->where(['id'=>$this->accessData['uid']])->find();
            $Personal['card'] = "";
            $Personal['state'] = "0";
            $Personal['account'] = "0";
            suc('success',$Personal);
        }
    }
    
    /**
     * @SWG\Post(
     * path="/Create_card_page",
     * tags={"Card"},
     * summary="编辑名片页面",
     * description="编辑名片页面-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function Create_card_page()
    {
        $card = db('card_card')->where(['user_id'=>$this->accessData['uid']])->find();
        
        if (!$card){
           err('还未创建名片'); 
        }
        suc('',$card);
    }
    
    
    /**
     * @SWG\Post(
     * path="/Create_card",
     * tags={"Card"},
     * summary="编辑我的名片",
     * description="编辑我的名片-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="name", description="昵称", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="image", description="头像", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="sex", description="性别1-男,2-女", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="school", description="大学", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="position", description="专业", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="schooling", description="年级", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="middle_school", description="高中", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="class_name", description="班级", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="countries", description="国籍", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="ethnic", description="民族", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="province", description="家乡", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="faith", description="信仰", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="birthday", description="生日", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="animal", description="属相", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="constellation", description="星座", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="height", description="身高", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="weight", description="体重", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="blood_type", description="血型", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="personality", description="个性标签", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="hobbies", description="兴趣爱好", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="idol", description="崇拜偶像", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="title", description="职务头衔", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="group", description="加入团体", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="honor", description="曾获荣誉", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="username", description="真实性名", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="number", description="个人学号", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="weixin", description="联系方式(QQ,微信)", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="content", description="个性签名", in="formData", required=false, type="integer"),
     * @SWG\Parameter(name="theme", description="名片主题(1,2,3,4)", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="img", description="上传照片", in="formData", required=false, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function Create_card()
    {
        $post = request()->only([
                        'image','name','sex','school','position','schooling','middle_school','class_name','countries',
                        'ethnic','province','faith','birthday','animal','constellation','height','weight','blood_type',
                        'personality','hobbies','idol','title','group','honor','username','number','weixin','content',
                        'theme','img'], 'post');

        $post['user_id'] = $this->accessData['uid'];
        #生成随机名片编号
        $post['card'] = rand(10,99999999999);

        #表单验证
        $result = $this->validate($post, 'Mingpian.create_card');
        
        if (true !== $result) {
            err($result);
        }
        
        #查询名片不存在增加，否则修改
        $my_card = db('card_card')->where(['user_id'=>$this->accessData['uid']])->find();
        
        if (!$my_card){
            $card = db('card_card')->insert($post);
        }else{
            $card = db('card_card')->where(['user_id'=>$this->accessData['uid']])->update($post);
        }
        
        if (!$card){
            err('empty');
        }else{
            suc('success');
        }
    }
    
    /**
     * @SWG\Post(
     * path="/Identity",
     * tags={"Create_card"},
     * summary="身份认证",
     * description="身份认证-吴辉",
     * @SWG\Parameter(name="mstr", description="加密KEY", in="formData", required=true, type="string"),
     * @SWG\Parameter(name="tel", description="手机号码", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="code", description="验证码", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="type", description="认证类型", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="front", description="证件正面照", in="formData", required=true, type="integer"),
     * @SWG\Parameter(name="reverse", description="证件反面照", in="formData", required=true, type="integer"),
     *
     * @SWG\Response(
     * response=200,
     * description="成功返回数据",
     * )
     * )
     */
    public function Identity()
    {
        $post = request()->only(['tel','code','type','front','reverse'],'post');
        
        //短信验证
        //表单验证
        $result = $this->validate($post, 'Mingpian.identity');
        
        if (true !== $result) {
            err($result);
        }
    }
	
	public function prayer(){
		
		$post = request()->only(['light_id','type_user_id','content'],'post');
		
		$light = db("card_gift")->where(["id"=>$post["light_id"]])->find();
		if(empty($light)){
			err("魔力灯不存在");
		}
		
		$uid = $this->accessData['uid'];
		$me = db("card_card")->where(["user_id"=>$uid])->find();
		
		if($me["account"] < $light["money"]){
			err("乐豆不足");
		}
		
		$data = [];
		$data["user_id"] = $uid;
		$data["type_user_id"] = $post["type_user_id"];
		$data["gift_id"] = $post["light_id"];
		$data["title"] = $light["name"];
		$data["img"] = $light["image"];
		$data["money"] = $light["money"];
		$data["content"] = $post["content"];
		$data["create_time"] = date("Y-m-d H:i:s",time());
		
		$res = db("card_gift_giving")->insert($data);
		if($res){
			$meMoney = $me["account"] - $light["money"];
			db("card_card")->where(["user_id"=>$uid])->update(["account"=>$meMoney]);
			suc("操作成功");
		}else{
			err("操作成功");
		}
	}
}








